package znbkListBj.presenter;

import com.example.lancooznbk.R;

import znbkChangeNote.api.DeleteNoteApi;
import znbkChangeNote.entity.DeleteNoteEntity;
import znbkListBj.api.NotesListApi;
import znbkListBj.entity.NoteListEntity;
import znbkListBj.INotePresenter;
import znbkListBj.INoteListView;
import znbkTestPaperBj.activity.NotePaperActivity;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;

import com.example.lancooznbk.view.base.BaseFragment;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.util.List;

/**
 * Created by Administrator on 2018\3\16 0016.
 */

public class NoteListPresenter implements INotePresenter {

    private INoteListView mView;
    private int lastPosition = 0;

    private int pageIndex = 1;
    private boolean fromSearch = false;

    private HttpOnNextListener<NoteListEntity> getNotesListener = new HttpOnNextListener<NoteListEntity>() {
        @Override
        public void onNext(NoteListEntity noteListEntity) {
            if (noteListEntity.getTotalCount() > 0){
                mView.hideLoading();

                List<NoteListEntity.NoteBean> modelList = noteListEntity.getNote();
                for (int i = 0 ; i < modelList.size() ; i ++){ //预处理题库返回不合理的地方
                    String summary = modelList.get(i).getTitle();
                    String regexStr = "<[^>]*>"; //去除所有的标签
                    summary = summary.replaceAll(regexStr, "");

                  /*  summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/
                  if(summary.equals("")){
                      summary = "暂无简介";
                  }else{
                      if (summary.length() > 100){
                          summary = summary.substring(0,100);
                      }
                  }

                    modelList.get(i).setTitle(summary);
                }

                if (pageIndex == 1) {
                    mView.loadData(noteListEntity);
                    lastPosition = 0;
                }else {
                    mView.loadMoreData(noteListEntity);
                }


            }else {
                mView.showNoData(fromSearch ? mView.getHolderActivity().getString(R.string.search_no_notes_list):
                        mView.getHolderActivity().getString(R.string.no_notes_list));
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.showNetError();
        }
    };
    private HttpOnNextListener<String> deleteNoteListener = new HttpOnNextListener<String>() {

        @Override
        public void onNext(String s) {
            if(s.equals("true")){
                mView.delFavouriteSucceed();
            }else{
                ToastUtils.showShort(mView.getHolderActivity().getString(R.string.del_failed));
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
//            Log.e("===222222000000==","");
//            ToastUtils.showShort(mView.getHolderActivity().getString(R.string.del_failed22));
        }
    };

    public NoteListPresenter(INoteListView mView) {
        this.mView = mView;
    }

    private void loadData(){
        fromSearch = false;

        NotesListApi api = new NotesListApi(getNotesListener, (RxFragment) mView);
        api.setSearchInfo("");
        api.setPageNumber(pageIndex);
        api.setPageSize(lastPosition + 10);
        api.setQuesID("");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getData(boolean isShowing) {
        if (isShowing) mView.showLoading();
        pageIndex = 1;
        loadData();
    }

    @Override
    public void getMoreData() {
        pageIndex = pageIndex + 1;
        loadData();
    }

    @Override
    public void searchFavouriteItem(String str) {
        fromSearch = true;
        mView.showLoading();
        pageIndex = 1;
        NotesListApi api = new NotesListApi(getNotesListener, (RxFragment) mView);
        api.setQuesID("");
        api.setSearchInfo(str);
        api.setPageNumber(pageIndex);
        api.setPageSize(lastPosition + 10);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }



    @Override
    public void delItemFavourite(int position, NoteListEntity.NoteBean noteBean) {
        DeleteNoteApi api = new DeleteNoteApi(deleteNoteListener, (BaseFragment) mView);
        DeleteNoteEntity deleteNoteEntity = new DeleteNoteEntity(UserInfo.StuId,UserInfo.SubjectId,
                UserInfo.StageNo,noteBean.getNoteID(),noteBean.getResType(),noteBean.getQuesID());
        api.setmDeleteNoteEntity(deleteNoteEntity);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void openFavoriteItemDetail(int position, NoteListEntity.NoteBean noteBean) {
        lastPosition = position;
        ZnbkPaperInfor.StuID = noteBean.getStuID();
        ZnbkPaperInfor.SubjectID = noteBean.getSubjectID();
        ZnbkPaperInfor.StageNo = noteBean.getStageNo();
        ZnbkPaperInfor.QuesID = noteBean.getQuesID();
        ZnbkPaperInfor.NoteId = noteBean.getNoteID();
        //TODO 在这里实现跳转逻辑 如果想传递唯一标识的参数可以传递 @PaperDef int FROM_FAVOURITE_REVIEW
        NotePaperActivity.launch((BaseFragment) mView,noteBean, ZnbkPaperDef.FROM_NOTE);
//        Log.e("========QuesID======",""+favouriteListBean.getQuesID());

    }
}